package com.qianfeng.es.service.impl;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import com.qianfeng.es.service.QueryService;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchScrollRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * Created by Jackiechan on 2022/9/7 22:26
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
@Service
public class QueryServiceImpl implements QueryService {
    private RestHighLevelClient restHighLevelClient;

    @Autowired
    public void setRestHighLevelClient(RestHighLevelClient restHighLevelClient) {
        this.restHighLevelClient = restHighLevelClient;
    }

    @Override
    public SearchResponse termQuery(String index, String filed, String value) throws Exception {
//        SearchRequest searchRequest = new SearchRequest(index);
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        //设置为term查询
//        searchSourceBuilder.query(QueryBuilders.termQuery(filed, value));
//        //设置条件
//        searchRequest.source(searchSourceBuilder);
//        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//        return response;
        return baseQuery(index, QueryBuilders.termQuery(filed, value));
    }

    @Override
    public SearchResponse termsQuery(String index, String filed, String... value) throws Exception {
//        SearchRequest searchRequest = new SearchRequest(index);
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        //设置为term查询
//        searchSourceBuilder.query(QueryBuilders.termsQuery(filed, value));
//        //设置条件
//        searchRequest.source(searchSourceBuilder);
//        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//        return response;
        return baseQuery(index, QueryBuilders.termsQuery(filed, value));
    }

    @Override
    public SearchResponse matchAllQuery(String index) throws Exception {
        return baseQuery(index, QueryBuilders.matchAllQuery());
    }

    @Override
    public SearchResponse matchQuery(String index, String filed, String value) throws Exception {
        return baseQuery(index, QueryBuilders.matchQuery(filed, value));
    }

    @Override
    public SearchResponse boolMatchQuery(String index, String filed, String value, Operator operator) throws Exception {
        return baseQuery(index, QueryBuilders.matchQuery(filed, value).operator(operator));
    }

    @Override
    public SearchResponse multiMatch(String index, String value, String... field) throws Exception {

        return baseQuery(index, QueryBuilders.multiMatchQuery(value, field));

    }

    @Override
    public SearchResponse idsMatch(String index, String... ids) throws Exception {
        return baseQuery(index, QueryBuilders.idsQuery().addIds(ids));
    }

    @Override
    public SearchResponse prfixMatch(String index, String field, String value) throws Exception {
        return baseQuery(index, QueryBuilders.prefixQuery(field,value));
    }

    @Override
    public SearchResponse FuzzyMatch(String index, String field, String value, int lenght) throws Exception {
        return baseQuery(index,QueryBuilders.fuzzyQuery(field,value).prefixLength(2));
    }

    @Override
    public SearchResponse wildCardMatch(String index, String field, String value) throws Exception {
            return baseQuery(index,QueryBuilders.wildcardQuery(field,value));
    }

    @Override
    public SearchResponse regexpMatch(String index, String field, String regexp) throws Exception {
        return baseQuery(index,QueryBuilders.regexpQuery(field,regexp));
    }

    @Override
    public SearchResponse CreateScrollQuery(String index, int size, QueryBuilder queryBuilder) throws Exception {
        SearchRequest searchRequest = new SearchRequest(index);
        //设置滚动查询
        searchRequest.scroll("1m");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //设置为term查询
        searchSourceBuilder.query(queryBuilder);
        //设置返回的条数
        searchSourceBuilder.size(size);
        //设置条件
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        return response;
    }

    @Override
    public SearchResponse scrollQuery(String scrolId) throws Exception {
        return restHighLevelClient.scroll( new SearchScrollRequest(scrolId),RequestOptions.DEFAULT);
    }

    @Override
    public SearchResponse boolQuery(String index, QueryBuilder queryBuilder) throws Exception {
        return baseQuery(index,queryBuilder);
    }

    @Override
    public SearchResponse hightLightQuery(String index, QueryBuilder queryBuilder, HighlightBuilder hightLightBuilder) throws Exception {
        SearchRequest searchRequest = new SearchRequest(index);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //设置为term查询
        searchSourceBuilder.query(queryBuilder);
        ///设置高亮
        searchSourceBuilder.highlighter(hightLightBuilder);
        //设置条件
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        return response;
    }


    public SearchResponse baseQuery(String index, QueryBuilder queryBuilder) throws IOException {
        SearchRequest searchRequest = new SearchRequest(index);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //设置为term查询
        searchSourceBuilder.query(queryBuilder);
        //设置条件
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        return response;
    }

}
